//
//  LcVideoPlayer.h
//  18Birdies
//
//  Created by James Chen on 2019/5/28.
//  Copyright © 2019 SquareValley. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAnimation.h>
#import <AVKit/AVKit.h>

@class BaseViewController, AVAsset;

typedef enum {
  VideoPlayerStateUnknow,
  VideoPlayerStateReady,
  VideoPlayerStatePlaying,
  VideoPlayerStatePaused,
  VideoPlayerStateFinished,
  VideoPlayerStateFailed,
} VideoPlayerState;

typedef void (^StateChangedCallback)(VideoPlayerState newState);
typedef void (^TimeChangedCallback)(double currentSeconds);
typedef void (^TimeControlStatusChangedCallback)(AVPlayerTimeControlStatus timeControlStatus);

@interface LcVideoPlayer : NSObject

+ (instancetype)videoPLayerWithUrl:(NSURL*)url
                          onScreen:(UIView*)screen;
+ (instancetype)videoPLayerWithUrl:(NSURL*)url
                         extension:(NSString*)extension
                          onScreen:(UIView*)screen;
+ (instancetype)videoPLayerWithAsset:(AVAsset *)asset
                            onScreen:(UIView *)screen;
- (void)free;

@property(nonatomic, copy) NSURL* url;
@property(nonatomic, copy) NSString* extension;
@property(nonatomic, copy) AVAsset* asset;

@property(nonatomic, assign) BOOL playWhenReady; // default is NO
@property(nonatomic, assign) BOOL replayWhenFinish; // default is NO
@property(nonatomic, assign) BOOL muted; // default is NO

@property(nonatomic, weak) UIView* screen;
- (void)resizeFrame; // call after screen changed frame

@property(nonatomic, assign, readonly) VideoPlayerState state;
@property(nonatomic, copy) StateChangedCallback stateChangedCallback;
@property(nonatomic, copy) TimeChangedCallback timeChangedCallback;
@property(nonatomic, copy) TimeControlStatusChangedCallback timeControlStatusChangedCallback;

@property(nonatomic, assign, readonly) double totalSeconds;
@property(nonatomic, assign) double currentSeconds;
@property(nonatomic, assign) float rate;
@property(nonatomic, copy) AVLayerVideoGravity videoGravity;

- (void)play;
- (void)pause;
- (NSArray<NSValue *>*)loadedTimeRanges;

@end

// BILogEvent
@interface LcVideoPlayer()

@property(nonatomic, assign) int replayedTimes; // count for replay times if replayWhenFinish == YES, from 0
@property(nonatomic, assign) BOOL screenChanged; // if screen changed, must be played in full or larger screen.

@end
